package me.exchange.com.modules.system.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import me.exchange.com.modules.system.domain.UrlConfig;
import me.exchange.com.modules.system.domain.User;
import me.exchange.com.modules.system.dto.UrlConfigDto;
import me.exchange.com.modules.system.dto.UrlConfigQueryCriteria;
import me.exchange.com.modules.system.mapper.UrlConfigMapper;
import me.exchange.com.modules.system.repository.UrlConfigRepository;
import me.exchange.com.modules.system.repository.UserRepository;
import me.exchange.com.modules.system.service.UrlConfigService;
import me.exchange.com.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author zjj
* @date 2020-05-04
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UrlConfigServiceImpl implements UrlConfigService {

    @Autowired
    private UrlConfigRepository urlConfigRepository;
    @Autowired
    private UrlConfigMapper urlConfigMapper;
    @Autowired
    private UserRepository userRepository;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(UrlConfigQueryCriteria criteria, Pageable pageable){
        Page<UrlConfig> page = urlConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(urlConfigMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<UrlConfigDto> queryAll(UrlConfigQueryCriteria criteria){
        Sort sort = criteria.getSort();
        return urlConfigMapper.toDto(urlConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort));
    }

    @Override
    //@Cacheable(key = "#p0")
    public UrlConfigDto findById(Long id) {
        UrlConfig urlConfig = urlConfigRepository.findById(id).orElseGet(UrlConfig::new);
        ValidationUtil.isNull(urlConfig.getId(),"UrlConfig","id",id);
        return urlConfigMapper.toDto(urlConfig);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public UrlConfigDto create(UrlConfig resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return urlConfigMapper.toDto(urlConfigRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(UrlConfig resources) {
        UrlConfig urlConfig = urlConfigRepository.findById(resources.getId()).orElseGet(UrlConfig::new);
        ValidationUtil.isNull( urlConfig.getId(),"UrlConfig","id",resources.getId());
        urlConfig.copy(resources);
        //resources =(UrlConfig)BeanUtil.merge(resources,urlConfig);
//        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        urlConfigRepository.save(urlConfig);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void delete(Long id) {
        UrlConfig urlConfig = urlConfigRepository.findById(id).orElseGet(UrlConfig::new);
        urlConfigRepository.save(urlConfig);
    }
    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        User user=userRepository.findByUsername(SecurityUtils.getUsername());
        for (Long id : ids) {
            UrlConfig urlConfig = urlConfigRepository.findById(id).orElseGet(UrlConfig::new);
            urlConfig.setCreateTime(new Timestamp(System.currentTimeMillis()));
            urlConfigRepository.save(urlConfig);
            //urlConfigRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UrlConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UrlConfigDto urlConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" webcode",  urlConfig.getWebcode());
            map.put(" createTime",  urlConfig.getCreateTime());
            map.put(" isPush",  urlConfig.getIsPush());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<UrlConfig> findAllOrderBy() {
        return urlConfigRepository.findAllOrderBy();
    }
}